package com.dongdongshop.consumer;

import com.dongdongshop.pojo.BeadmsgQueue;
import com.dongdongshop.service.BeadmsgQueueService;
import com.dongdongshop.service.FreemarkerService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
@RocketMQMessageListener(topic = "item-goodsId",consumerGroup = "freemarker-goodsId-1")
public class ItemGoodsIdConsumerMq implements RocketMQListener<String>,RocketMQPushConsumerLifecycleListener {
    @Autowired
    private RedisTemplate redisTemplate;

    @DubboReference
    private FreemarkerService freemarkerService;
    @DubboReference
    private BeadmsgQueueService beadmsgQueueService;

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                MessageExt message = list.get(0);
                String msgId = message.getMsgId();//消息id
                int reconsumeTimes = message.getReconsumeTimes();//重试次数
                String topic = message.getTopic();//消息主题
                String msg = new String(message.getBody());//消息实体


                Boolean aBoolean = redisTemplate.hasKey(message);
                try {
                    if (!aBoolean){
                        freemarkerService.createPage(msg);
                        redisTemplate.boundValueOps(message).set("1");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    if (reconsumeTimes<=3){
                        return ConsumeConcurrentlyStatus.RECONSUME_LATER;
                    }
                    BeadmsgQueue beadmsgQueue = new BeadmsgQueue();
                    beadmsgQueue.setBody(msg);
                    beadmsgQueue.setCreattime(new Date());
                    beadmsgQueue.setErrorMessage("页面生成失败"+e.getMessage());
                    beadmsgQueue.setMessageId(msgId);
                    beadmsgQueue.setTopic(topic);
                    beadmsgQueue.setStatus(0);
                    beadmsgQueueService.addBeadmsg(beadmsgQueue);

                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
    }

    @Override
    public void onMessage(String s) {

    }
}
